﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA
{
    public partial class fdPicLabel : UserControl
    {
        private Image _iconImage;
        private string _lblText="show Message";
        private bool _isActive = false;
        private bool _isRunning = false;
        private Font _lblFont;
        private System.Timers.Timer _timRunning = new System.Timers.Timer(500);
        private int _runningInterval = 500;
        public Image iconImage { get { return _iconImage; } set { _iconImage= pic_img.Image = value; reSizeInterface(); } }
        public string lblText { get { return _lblText; } set { lbl_Content.Text = _lblText = value; } }
        public bool IsActive{ get { return _isActive; } set { if (_isActive != value) { ChangeStatus(value); _isActive = value; } } }

        public Font LblFont { get { return _lblFont; } set { lbl_Content.Font = _lblFont = value; reSizeInterface(); } }

        public bool IsRunning { get => _isRunning; set => SetRunningStatus(_isRunning = value); }
        public int RunningInterval { get => _runningInterval; set => TimRunning.Interval=(_runningInterval = value); }
        public System.Timers.Timer TimRunning { get => _timRunning;  }

        public fdPicLabel()
        {
            InitializeComponent();
            _lblFont = lbl_Content.Font;
        }

        private void fdPicLabel_Resize(object sender, EventArgs e)
        {
            reSizeInterface();
        }
        private void reSizeInterface()
        {
            if (pic_img.Image == null) pic_img.Width = 0;
            lbl_Content.Location = new Point(pic_img.Width + 2, 0);
            lbl_Content.Size = new Size(this.Width - pic_img.Width - 2, this.Height);
        }

        private void fdPicLabel_Load(object sender, EventArgs e)
        {
            pic_img.Location = new Point(0 , 0);
            lbl_Content.BackColor = this.BackColor = this.Parent.BackColor;
            lbl_Content.Text = _lblText;
           // TimRunning.Interval = 500;
            _timRunning.Elapsed += _timRunning_Elapsed;
            reSizeInterface();
        }

        private void _timRunning_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Invoke((EventHandler)delegate
            {
                if (pic_img.Top == 0)
                    pic_img.Top = -pic_img.Height / 2;
                else
                    pic_img.Top = 0;
            });
        }

        private void _timRunning_Tick(object sender, EventArgs e)
        {
            this.Invoke((EventHandler)delegate
            {
                if (pic_img.Top == 0)
                    pic_img.Top = -pic_img.Height / 2;
                else
                    pic_img.Top = 0;
            });
        }

        private void ChangeStatus(bool active)
        {
            this.Invoke((EventHandler)delegate
            {
                if (active)
                    pic_img.Top = -pic_img.Height / 2;
                else
                    pic_img.Top = 0;
            });
        }

        private void SetRunningStatus(bool run)
        {
            if (TimRunning.Enabled != run)
            {
                if (run)
                    _timRunning.Start();
                else
                    _timRunning.Stop();
            }

        }
        private void fdPicLabel_BackColorChanged(object sender, EventArgs e)
        {
            lbl_Content.BackColor = this.BackColor = this.Parent.BackColor;
        }

    }
}
